home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-16 | 17.6 KB | 690 lines | [TEXT/CWIE] |
- /******************************************************************************
- ** **
- ** Module: PPTestApp.cp **
- ** **
- ** **
- ** Purpose: Test harness for PowerPlay **
- ** **
- ** Author: Jamie Osborne **
- ** **
- ** Copyright (C) 1996 Apple Computer, Inc. All rights reserved. **
- ** **
- ** **
- *****************************************************************************/
-
- #define VERBOSE 0
-
- #include "NSTestApp.h"
- #include "CPlayerWindow.h"
- #include <LGrowZone.h>
- #include <LWindow.h>
- #include <PP_Messages.h>
- #include <PP_Resources.h>
- #include <PPobClasses.h>
- #include <UDrawingState.h>
- #include <UMemoryMgr.h>
- #include <URegistrar.h>
- #include <LEditField.h>
- #include <iostream>
- #include <sioux.h>
- #include <stdio.h>
- #include <UModalDialogs.h>
- #include <LArrayIterator.h>
- #include <LString.h>
-
- #include <OpenTptAppleTalk.h>
-
- #include <NetSprocket.h>
-
- // put declarations for resource ids (ResIDTs) here
-
- const CommandT cmd_Advertise = 2000;
- const CommandT cmd_Join = 2001;
- const CommandT cmd_Unadvertise = 2002;
- const CommandT cmd_Start = 2003;
- const CommandT cmd_TestGroups = 2004;
-
- enum {kKeyboardState = 600, kGameStart, kGameEnd, kTestGroups};
- Boolean IsPressed(unsigned short k);
- pascal Boolean MyJoinRequestHandler(NSpGameReference inGame,
- NSpJoinRequestMessage *inMessage, void* inContext,
- Str255 outReason);
-
- const char *kJoinRequestString = "DirectX- the Road to Nowhere";
- Boolean gGameOver;
- // ===========================================================================
- // • Main Program
- // ===========================================================================
-
- pascal Boolean MyJoinRequestHandler(NSpGameReference inGame,
- NSpJoinRequestMessage *inMessage, void* inContext,
- Str255 outReason)
- {
- LString::CopyPStr("\pYou forgot to say 'Simon says.'", outReason);
-
- return BlocksAreEqual(inMessage->customData, kJoinRequestString,
- inMessage->customDataLen);
- }
-
- void main(void)
- {
- SIOUXSettings.autocloseonquit = true;
- SIOUXSettings.asktosaveonclose = false;
- SIOUXSettings.initializeTB = false;
- SIOUXSettings.setupmenus = false;
- SIOUXSettings.standalone = false;
- // Set Debugging options
- SetDebugThrow_(debugAction_SourceDebugger);
- SetDebugSignal_(debugAction_SourceDebugger);
- InitializeHeap(3); // Initialize Memory Manager
- // Parameter is number of Master Pointer
- // blocks to allocate
-
- // Initialize standard Toolbox managers
- UQDGlobals::InitializeToolbox(&qd);
- gGameOver = false;
- new LGrowZone(20000); // Install a GrowZone function to catch
- // low memory situations.
- PPTestApp theApp; // replace this with your App type
-
- theApp.Run();
- }
-
-
- // ---------------------------------------------------------------------------
- // • CPPStarterApp // replace this with your App type
- // ---------------------------------------------------------------------------
- // Constructor
-
- PPTestApp::PPTestApp()
- {
- // Register functions to create core PowerPlant classes
- bAdvertising = false;
- bRunning = false;
- mPlayerCount = 0;
- theGame = NULL;
- RegisterAllPPClasses();
- URegistrar::RegisterClass(CPlayerWindow::class_ID, CPlayerWindow::CreatePlayerWindowStream);
- gPulseBit = 0;
-
- // Initialize our player list
- CPlayerListComparator *theComparator = new CPlayerListComparator();
- ThrowIfNULL_(theComparator);
- mPlayerList = new LArray(sizeof(PlayerListItem), theComparator, true);
- ThrowIfNULL_(mPlayerList);
-
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~PPTestApp // replace this with your App type
- // ---------------------------------------------------------------------------
- // Destructor
- //
-
- PPTestApp::~PPTestApp()
- {
- }
-
- // ---------------------------------------------------------------------------
- // • StartUp
- // ---------------------------------------------------------------------------
- // This function lets you do something when the application starts up.
- // For example, you could issue your own new command, or respond to a system
- // oDoc (open document) event.
-
- void
- PPTestApp::StartUp()
- {
- OSStatus err;
- UInt32 choice = 0;
-
- ObeyCommand(cmd_New, nil); // EXAMPLE, create a new window
-
- // Do some quick and dirty testing
- cout << "Starting up" << endl;
- err = NSpInitialize(500, 200000, 100, 'foob', 20);
- cout << "NSpInitialize: " << err << endl;
- err = NSpInstallJoinRequestHandler(&MyJoinRequestHandler, NULL);
- cout << "NSpInstallJoinRequestHandler returned " << err << endl;
-
-
- }
-
-
- Boolean IsPressed(unsigned short k)
- // k = any keyboard scan code, 0-127
- {
- KeyMap map;
- unsigned char km[16];
-
- GetKeys(map);
- BlockMove(map, km, 16);
-
- Boolean pressed;
- pressed = ( ( km[k>>3] >> (k & 7) ) & 1);
-
- return pressed;
- }
- int gLastTickCount = 0;
-
- void
- PPTestApp::ProcessNextEvent()
- {
- EventRecord macEvent;
- NSpMessageHeader *theMessage;
- OSStatus status;
- if (IsOnDuty()) {
-
- // Calling OSEventAvail with a zero event mask will always
- // pass back a null event. However, it fills the EventRecord
- // with the information we need to set the cursor shape--
- // the mouse location in global coordinates and the state
- // of the modifier keys.
-
- ::OSEventAvail(0, &macEvent);
- AdjustCursor(macEvent);
- }
-
- // Retrieve the next event. Context switch could happen here.
-
- Boolean gotEvent = ::WaitNextEvent(everyEvent, &macEvent, 0,
- mMouseRgnH);
-
-
- Boolean SIOUXDidEvent = SIOUXHandleOneEvent(&macEvent);
- if (!SIOUXDidEvent)
- {
- SetUpdateCommandStatus(false);
-
- // Let Attachments process the event. Continue with normal
- // event dispatching unless suppressed by an Attachment.
-
- if (LAttachable::ExecuteAttachments(msg_Event, &macEvent)) {
- if (gotEvent) {
- DispatchEvent(macEvent);
- } else {
- UseIdleTime(macEvent);
- }
- }
- }
- // Repeaters get time after every event
- LPeriodical::DevoteTimeToRepeaters(macEvent);
-
- // Update status of menu items
- if (IsOnDuty() && GetUpdateCommandStatus()) {
- UpdateMenus();
- }
-
- if (theGame && bRunning /*&& IsPressed(122)*/)
- {
- UInt32 count = TickCount();
- if (count >= gLastTickCount)
- {
- TKeyState state;
-
- gLastTickCount = count + 3;
-
- NSpClearMessageHeader((NSpMessageHeader *)&state);
- state.h.what = kKeyboardState;
- state.h.to = kNSpAllPlayers;
- state.h.messageLen = sizeof(TKeyState);
-
- state.keyMap[0] = IsPressed(0x7e) ? 1 : 0;
- state.keyMap[1] = IsPressed(0x7d) ? 1 : 0;
- state.keyMap[2] = IsPressed(0x7c) ? 1 : 0;
- state.keyMap[3] = IsPressed(0x7b) ? 1 : 0;
- state.pulseBit = gPulseBit;
- gPulseBit = (gPulseBit == 1) ? 0 : 1;
-
- // if (state.keyMap[0] != 0)
- // NSpMessage_Send(theGame, (NSpMessageHeader *) &state, kNSpSendFlag_Normal | kNSpSendFlag_SelfSend);
- status = NSpMessage_SendTo(theGame, kNSpAllPlayers,
- kKeyboardState, state.keyMap, 16,
- kNSpSendFlag_Normal | kNSpSendFlag_SelfSend);
-
- }
- }
-
- if (theGame)
- {
- while ((theMessage = NSpMessage_Get(theGame)) != NULL)
- {
- HandleGameEvent(theMessage);
- NSpMessage_Release(theGame, (NSpMessageHeader *)theMessage);
- if (gGameOver)
- {
- status = NSpGame_Dispose(theGame, 0);
- cout << "NSpGame_Dispose returned " << status << endl;
- theGame = NULL;
- bRunning = false;
- ObeyCommand(cmd_Quit, NULL);
- }
- }
- }
-
- }
-
-
- void PPTestApp::HandleGameEvent(NSpMessageHeader *inEvent)
- {
- #if VERBOSE
- cout << "Handling event:";
- cout << "\twhat = " << inEvent->what << endl;
- cout << "\twhen = " << inEvent->when << endl;
- cout << "\tfrom = " << inEvent->from << endl;
- cout << "\tto = " << inEvent->to << endl;
- cout << "\tid = " << inEvent->id << endl;
- cout << "\tdataLen = " << inEvent->dataLen << endl;
- cout << "\tcookie = " << inEvent->cookie << endl;
- #endif
- switch (inEvent->what)
- {
- case kNSpJoinApproved:
- break;
- case kNSpJoinDenied:
- break;
- case kNSpPlayerJoined:
- HandlePlayerJoined((NSpPlayerJoinedMessage *)inEvent);
- break;
- case kNSpPlayerLeft:
- HandlePlayerLeft((NSpPlayerLeftMessage *)inEvent);
- break;
- case kKeyboardState:
- HandleKeyboardState((TKeyState *)inEvent);
- break;
- case kGameStart:
- HandleGameStart(inEvent);
- break;
- case kGameEnd:
- HandleGameEnd(inEvent);
- break;
- case kTestGroups:
- HandleTestGroups(inEvent);
- break;
- default:
- cout << "Unhandled game event:" << inEvent->what << endl;
- break;
- }
- }
-
- void PPTestApp::HandlePlayerJoined(NSpPlayerJoinedMessage *inEvent)
- {
- PlayerListItem item;
- NSpMessageHeader theMessage;
- OSStatus status;
-
- cout << "In HandlePlayerJoined" << endl;
-
- if (inEvent->playerInfo.id == NSpPlayer_GetMyID(theGame))
- return;
-
- if (bRunning)
- {
- if (bHost)
- {
- NSpClearMessageHeader(&theMessage);
- theMessage.what = kGameStart;
- theMessage.to = inEvent->playerInfo.id;
- theMessage.messageLen = sizeof(NSpMessageHeader);
-
- status = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
- ThrowIfOSErr_(status);
- }
-
- LWindow *window = LWindow::CreateWindow(1002, this);
- window->SetDescriptor(inEvent->playerInfo.name);
- window->Show();
- item.player = inEvent->playerInfo.id;
- item.window = window;
- mPlayerList->InsertItemsAt(1, 0, &item);
- }
-
- }
-
- void PPTestApp::HandlePlayerLeft(NSpPlayerLeftMessage *inEvent)
- {
- cout << "In HandlePlayerLeft" << endl;
-
- ArrayIndexT index = mPlayerList->FetchIndexOfKey(&inEvent->playerID);
- PlayerListItem item;
- if (mPlayerList->FetchItemAt(index, &item))
- item.window->DoClose();
-
- mPlayerCount--;
- }
-
- void PPTestApp::HandleGameStart(NSpMessageHeader *inEvent)
- {
- UInt32 i;
- LWindow *window;
- bRunning = true;
- PlayerListItem item;
- LStr255 title;
- OSStatus err;
-
- NSpPlayerEnumerationPtr thePlayers;
- NSpPlayerInfoPtr playerInfo;
-
- err = NSpPlayer_GetEnumeration(theGame, &thePlayers);
- if (err == noErr)
- {
- for (i = 0; i < thePlayers->count; i++)
- {
- playerInfo = thePlayers->playerInfo[i];
- window = LWindow::CreateWindow(1002, this);
- window->SetDescriptor(playerInfo->name);
- window->Show();
- item.player = playerInfo->id;
- item.window = window;
-
- mPlayerList->InsertItemsAt(1, 0, &item);
-
- }
-
- NSpPlayer_ReleaseEnumeration(theGame, thePlayers);
- }
- else
- cout << "NSpPlayer_GetEnumeration returned " << err << endl;
- }
-
- void PPTestApp::HandleGamePause(NSpMessageHeader *inEvent)
- {
- cout << "In HandleGamePause" << endl;
- }
-
- void PPTestApp::HandleGameEnd(NSpMessageHeader *inEvent)
- {
- cout << "In HandleGameEnd" << endl;
- PlayerListItem item;
- LArrayIterator iter(*mPlayerList);
-
- while(iter.Next(&item))
- {
- item.window->DoClose();
- mPlayerList->Remove(&item);
- }
-
- gGameOver = true;
- }
-
- void PPTestApp::HandleTestGroups(NSpMessageHeader *inEvent)
- {
- cout << "\t!!Received a message to group " << inEvent->to << endl;
- }
-
- void PPTestApp::HandleKeyboardState(TKeyState *inEvent)
- {
- PlayerListItem info;
- ArrayIndexT index;
-
- UInt32 time = NSpGetCurrentTimeStamp(theGame)/* - inEvent->h.when*/;
- index = mPlayerList->FetchIndexOfKey(&inEvent->h.from);
- #if DEBUG
- if (index == LArray::index_Bad)
- cout << "Got a bad player list index in HandleKeyboardState" << endl;
- #endif
- if (mPlayerList->FetchItemAt(index, &info))
- {
- CPlayerWindow *theWindow = (CPlayerWindow *)info.window;
-
- if (theWindow)
- {
- theWindow->mUp = inEvent->keyMap[0];
- theWindow->mDown = inEvent->keyMap[1];
- theWindow->mLeft = inEvent->keyMap[2];
- theWindow->mRight = inEvent->keyMap[3];
- theWindow->mPulse = inEvent->pulseBit;
- theWindow->mTransitTime = time;
- theWindow->Refresh();
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • ObeyCommand
- // ---------------------------------------------------------------------------
- // Respond to commands
-
- NSpProtocolListReference theList;
-
- Boolean
- PPTestApp::ObeyCommand(
- CommandT inCommand,
- void *ioParam)
- {
- Boolean cmdHandled = true;
- OSStatus err = noErr;
- Str255 name;
- Str255 password;
- Str255 gameName;
- UInt16 port = 3333;
- NSpMessageHeader theMessage;
-
- switch (inCommand) {
-
- // Deal with command messages (defined in PP_Messages.h).
- // Any that you don't handle will be passed to LApplication
-
- case cmd_Advertise:
- {
- UInt32 protocols = 0;
- LString::CopyPStr("\pTest1", gameName);
- LString::CopyPStr("\pbarney", password);
- LString::CopyPStr("\pDarmok", name);
-
- NSpProtocolListReference theList;
-
- NSpProtocolReference theRef = NSpProtocol_CreateAppleTalk(gameName, "\pbandersnatch", 10000, 1);
-
- err = NSpProtocolList_New(theRef, &theList);
- if (err != noErr)
- {
- cout << "NSpProtocolListRef_New returned an error! " << err << endl;
- return true;
- }
-
- if (!NSpDoModalHostDialog(theList, gameName, name, password, NULL))
- return true;
- err = NSpGame_Host(&theGame, theList, 12, gameName, password, name, 0, kNSpClientServer, 0);
- if (err != noErr)
- {
- cout << "NSpGame_New returned an error! " << err << endl;
- return true;
- }
-
- bAdvertising = true;
- bHost = true;
- }
- break;
- case cmd_Unadvertise:
- if (bAdvertising)
- {
- // err = NSpGame_EnableAdvertising(theGame, theList);
- cout << "NSpGame_StopHosting game (expect 0): " << err << endl;
-
- NSpProtocolList_Dispose(theList);
-
- if (err == noErr)
- bAdvertising = false;
- }
- break;
- case cmd_Join:
- {
- NSpAddressReference theAddress;
- LString::CopyPStr("\pDarmok", name);
- LString::CopyPStr("\pbarney", password);
-
- theAddress = NSpDoModalJoinDialog("\pbandersnatch", "\pGame servers:", name, password, NULL);
- cout << "DoModalJoinWithName returned an address ref of " << theAddress << endl;
- if (theAddress != NULL)
- {
- err = NSpGame_Join(&theGame, theAddress, name, password, 0, (void*) kJoinRequestString, 28,0);
-
- cout << "NSpGame_Join returned err: " << err << endl;
- NSpReleaseAddressReference(theAddress);
- }
- bHost = false;
- bAdvertising = false;
- }
- break;
- case cmd_Start:
- NSpClearMessageHeader(&theMessage);
- theMessage.what = kGameStart;
- theMessage.to = 0;
- theMessage.messageLen = sizeof(NSpMessageHeader);
-
- NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered | kNSpSendFlag_SelfSend);
- // bRunning = true;
-
- break;
- case cmd_TestGroups:
- NSpGroupID theGroup;
- NSpClearMessageHeader(&theMessage);
- theMessage.what = kTestGroups;
- theMessage.messageLen = sizeof(NSpMessageHeader);
-
- err = NSpGroup_New(theGame, &theGroup);
- cout << "NSpGroup_New returned: " << err << endl;
- if (!err)
- cout << "\tCreated group number " << theGroup << endl;
-
- theMessage.to = theGroup;
-
- err = NSpGroup_AddPlayer(theGame, theGroup, 1);
- cout << "NSpGroup_AddPlayer returned " << err << endl;
-
- err = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
-
- err = NSpGroup_AddPlayer(theGame, theGroup, 2);
- cout << "NSpGroup_AddPlayer returned " << err << endl;
-
- err = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
-
- err = NSpGroup_RemovePlayer(theGame, theGroup, 1);
- cout << "NSpGroup_RemovePlayer returned " << err << endl;
-
- err = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
-
- err = NSpGroup_Dispose(theGame, theGroup);
- cout << "NSpGroup_Dispose returned: " << err << endl;
- if (!err)
- cout << "\tDeleted group number " << theGroup << endl;
-
- err = NSpGroup_AddPlayer(theGame, theGroup, 2);
- cout << "NSpGroup_AddPlayer returned (should be err)" << err << endl;
-
- break;
- case cmd_Quit:
- if (theGame)
- {
- if (bHost)
- {
- NSpClearMessageHeader(&theMessage);
- theMessage.what = kGameEnd;
- theMessage.to = 0;
- theMessage.messageLen = sizeof(NSpMessageHeader);
-
- NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
- }
- NSpGame_Dispose(theGame, kNSpGameFlag_ForceTerminateGame);
- theGame = NULL;
- }
- bRunning = false;
- LApplication::ObeyCommand(inCommand, ioParam);
- default:
- cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
- break;
- }
-
- return cmdHandled;
- }
-
- // ---------------------------------------------------------------------------
- // • FindCommandStatus
- // ---------------------------------------------------------------------------
- // This function enables menu commands.
- //
-
- void
- PPTestApp::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName)
- {
-
- switch (inCommand) {
-
- // Return menu item status according to command messages.
- // Any that you don't handle will be passed to LApplication
-
- case cmd_Advertise:
- outEnabled = true;
- break;
- case cmd_Unadvertise:
- if (bAdvertising == true)
- outEnabled = true;
- else
- outEnabled = false;
- break;
- case cmd_Join:
- outEnabled = true;
- break;
- case cmd_Start:
- outEnabled = true;
- break;
- case cmd_TestGroups:
- outEnabled = true;
- break;
- default:
- LApplication::FindCommandStatus(inCommand, outEnabled,
- outUsesMark, outMark, outName);
- break;
- }
- }
-
-
- Int32
- CPlayerListComparator::Compare(
- const void* inItemOne,
- const void* inItemTwo,
- Uint32 /* inSizeOne */,
- Uint32 /* inSizeTwo */) const
- {
- return ( (((PlayerListItem *)inItemOne)->player) - (((PlayerListItem *)inItemTwo)->player) );
- }
-
-
- Boolean
- CPlayerListComparator::IsEqualTo(
- const void* inItemOne,
- const void* inItemTwo,
- Uint32 /* inSizeOne */,
- Uint32 /* inSizeTwo */) const
- {
- return ( (((PlayerListItem *)inItemOne)->player) == (((PlayerListItem *)inItemTwo)->player) );
- }
-
-
- Int32
- CPlayerListComparator::CompareToKey(
- const void* inItem ,
- Uint32 /* inSize */,
- const void* inKey ) const
- {
- return ( (((PlayerListItem *)inItem)->player) - (*(NSpPlayerID *)inKey) );
- }
-
-
- Boolean
- CPlayerListComparator::IsEqualToKey(
- const void* inItem,
- Uint32 inSize,
- const void* inKey) const
- {
- return ( (((PlayerListItem *)inItem)->player) == (*(NSpPlayerID *)inKey) );
- }
-